﻿function getArea(depth, areaSysNo) {
    var soSysNo = $("#SOSYSNO").val();
    var id = "#WebSiteSysNo" + soSysNo;
    var WebSiteSysNo = $(id).html();
    $.BENLAI.ajax({
        type: "POST",
        url: "/City/Area?depth=" + depth + "&areaSysNo=" + areaSysNo + "&WebSiteSysNo=" + WebSiteSysNo,
        cache: false,
        async: false,
        success: function (data) {
            if (AjaxValidate(data)) {
                $("#area").html(data.html);
            }
        },
        error: function (e) {
            alert("加载地区信息失败，请刷新页面重试");
            $("#area").html("<font color='red'>加载地区信息失败，请刷新页面重试</font>");
        }
    });
}

function showInvoiceWinJudge(soSysNo, soId,money) {
    $.BENLAI.ajax({
        type: "POST",
        url: "/UserHome/GetInvoice?SoSysNo=" + soSysNo + "&SoId=" + soId,
        cache: false,
        async: false,
        success: function (data) {
            if (AjaxValidate(data) && data != "") {
                if (data.error == "0") {
                    var dataIIO = data.data;
                    if (dataIIO.type == 1) {
                        var imm = dataIIO.invoiceMMInfo;
                        ShowBack();
                        $("#applyInvoiceInfo").show();
                        $("#editInvoiceMsg").show();

                        $("#editInvoice").hide();
                        $("#sureInvoice").hide();
                        $("#invoiceInfoClose").show();

                        $("#invoiceInfoTitle").html(imm.InvoinceName);
                        $("#invoiceInfoData").html(imm.InvoinceProductName);
                        $("#invoiceInfoAddr").html(imm.InvoinceAddress);
                        $("#invoiceInfoUserName").html(imm.InvoinceCustomerName);
                        $("#invoiceInfoPhone").html(imm.InvoincePhone);

                        $("#invoiceMoney").html(dataIIO.money.toFixed(2) + "元");
                        $("#invoiceInfoMoney").html(dataIIO.money.toFixed(2) + "元");
                    } else {

                        showInvoiceWin();
                        $("#sureInvoice").show();
                        $("#invoiceInfoClose").hide();
                        $("#invoiceMoney").html(dataIIO.money.toFixed(2) + "元");
                        $("#invoiceInfoMoney").html(dataIIO.money.toFixed(2) + "元");
                    }
                }
                else {
                    alert(data.msg);
                }
            }
            else {
                return false;
            }
        },
        error: function () {
            return false;
        }
    });
    $("#SOSYSNO").val(soSysNo);
    $("#SOID").val(soId);
    getArea(0, 0);
}


function showInvoiceWin() {
    ShowBack();
    $("#applyInvoice").show();
}

function AreaIsEmpty() {
    if ($("#district").val() == "0" || $("#district").val() == "" || $("#district").val() == undefined) {
        $("#areamsg").show();
        $("#district").focus();
        return false;
    } else {
        $("#areamsg").hide();
        return true;
    }
}


$(function () {
    $("#InvoiceSubDiv").CreateController({
        elements: {

        },
        events: {
            '#closeInvoice click': 'hideInvoiceWin',
            '#cancelInvoice click': 'hideInvoiceWin',
            '#submit click': 'ShowSubmitWin',
            '#closeInvoiceInfo click': 'hideInvoiceInfoWin',
            '#editInvoice click': 'EditInvoice',

            '#txtStreet keyup': 'DetailIsEmpty',
            '#txtStreet paste': 'DetailIsEmpty',
            '#txtZip keyup': 'PostIsInvalid',
            '#txtZip paste': 'PostIsInvalid',
            '#txtUserName keyup': 'NameIsEmpty',
            '#txtUserName paste': 'NameIsEmpty',
            '#txtCellphone keyup': 'TelIsInvalid',
            '#txtCellphone paste': 'TelIsInvalid',
            '#txtTelZone keyup': 'TelIsInvalid',
            '#txtTelZone paste': 'TelIsInvalid',
            '#txtTelephone keyup': 'TelIsInvalid',
            '#txtTelephone paste': 'TelIsInvalid',
            '#txtTelExtension keyup': 'TelIsInvalid',
            '#txtTelExtension paste': 'TelIsInvalid',
            'input[name=invoiceTitle] change': 'InvoiceTitleIsEmpty',
            '#txtInvoiceTitle keyup': 'InvoiceTitleIsEmpty',
            '#txtInvoiceTitle paste': 'InvoiceTitleIsEmpty',

            '#txtTelephone focus': 'TelePhoneFocus',
            '#txtTelephone blur': 'TelePhoneBlur',
            '#txtTelZone focus': 'TelZoneFocus',
            '#txtTelZone blur': 'TelZoneBlur',
            '#txtTelExtension focus': 'TelExtensionFocus',
            '#txtTelExtension blur': 'TelExtensionBlur',

            '#sureInvoice click': 'ConfirmInvoice',
            "#invoiceInfoClose click": 'hideInvoiceInfoWin'
        },
        init: function () {
            getArea(0, 0,0);
        },
        //----------------固定电话------------------------
        TelePhoneFocus: function () {
            if ($.trim($("#txtTelephone").val()) == "电话号码") {
                $("#txtTelephone").val("");
            }
        },
        TelePhoneBlur: function () {
            if (!$.trim($("#txtTelephone").val())) {
                $("#txtTelephone").val("电话号码");
            }
        },
        TelZoneFocus: function () {
            if ($.trim($("#txtTelZone").val()) == "区号") {
                $("#txtTelZone").val("");
            }
        },
        TelZoneBlur: function () {
            if (!$.trim($("#txtTelZone").val())) {
                $("#txtTelZone").val("区号");
            }
        },
        TelExtensionFocus: function () {
            if ($.trim($("#txtTelExtension").val()) == "分机号(可选)") {
                $("#txtTelExtension").val("");
            }
        },
        TelExtensionBlur: function () {
            if (!$.trim($("#txtTelExtension").val())) {
                $("#txtTelExtension").val("分机号(可选)");
            }
        },
        //----------------验证------------------------
        DetailIsEmpty: function () {
            if ($.trim($("#txtStreet").val()) == "") {
                $("#detailmsg").show();
                $("#txtStreet").focus();
                return false;
            } else {
                $("#detailmsg").hide();
                return true;
            }
        },
        PostIsInvalid: function () {
            var zipreg = /^\d{6}$/;
            if ($.trim($("#txtZip").val()) != "" && !zipreg.test($.trim($("#txtZip").val()))) {
                $("#postmsg").show();
                $("#txtZip").focus();
                return false;
            } else {
                $("#postmsg").hide();
                return true;
            }
        },
        NameIsEmpty: function () {
            if ($.trim($("#txtUserName").val()) == "") {
                $("#namemsg").show();
                $("#txtUserName").focus();
                return false;
            } else {
                $("#namemsg").hide();
                return true;
            }
        },
        TelIsInvalid: function () {
            var cellphonereg = /^1[34578][0-9]{9}$/;
            var phonereg = /^0?\d{3,4}$/;
            var phone1reg = /^\d{7,8}$/;
            var phone2reg = /^\d{1,4}$/;
            var regMail = /^\w+([-+."]\w+)*@@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

            if (($.trim($("#txtTelephone").val()) == "" || $.trim($("#txtTelephone").val()) == "电话号码") && $.trim($("#txtCellphone").val()) == "") {
                $("#telmsg").html("手机和电话至少填一项");
                $("#telmsg").show();
                return false;
            } else {
                $("#telmsg").hide();
            }
            var zoneTemp = $.trim($("#txtTelZone").val()) == "" ? "区号" : $.trim($("#txtTelZone").val());
            var teleTemp = $.trim($("#txtTelephone").val()) == "" ? "电话号码" : $.trim($("#txtTelephone").val());
            var extensionTemp = $.trim($("#txtTelExtension").val()) == "" ? "分机号(可选)" : $.trim($("#txtTelExtension").val());
            var trimPhone = zoneTemp + "-" + teleTemp + "-" + extensionTemp;

            if ($.trim($("#txtCellphone").val()) != "" && !cellphonereg.test($.trim($("#txtCellphone").val()))) {
                $("#telmsg").html("手机号码格式不正确");
                $("#telmsg").show();
                return false;
            } else {
                $("#telmsg").hide();
            }
            if (trimPhone != "区号-电话号码-分机号(可选)") {
                if (!phonereg.test($.trim($("#txtTelZone").val()))) {
                    $("#telmsg").html("区号格式不正确");
                    $("#telmsg").show();
                    return false;
                } else {
                    $("#telmsg").hide();
                }
                if (!phone1reg.test($.trim($("#txtTelephone").val()))) {
                    $("#telmsg").html("电话号码格式不正确");
                    $("#telmsg").show();
                    return false;
                } else {
                    $("#telmsg").hide();
                }
                if ($.trim($("#txtTelExtension").val()) != "分机号(可选)" && $.trim($("#txtTelExtension").val()) != "" && !phone2reg.test($.trim($("#txtTelExtension").val()))) {
                    $("#telmsg").html("分机号码格式不正确");
                    $("#telmsg").show();
                    return false;
                } else {
                    $("#telmsg").hide();
                }
            }
            var txtPhoneNumber = $.trim($("#txtPhone1").val());
            if (txtPhoneNumber == "区号") {
                txtPhoneNumber = "";
            } else {
                $("#telmsg").hide();
            }
            if (txtPhoneNumber != "" && txtPhoneNumber.substring(0, 1) != "0") {
                $("#telmsg").html("区号格式为0开头");
                $("#telmsg").show();
                return false;
            } else {
                $("#telmsg").hide();
            }
            return true;
        },
        InvoiceTitleIsEmpty: function () {
            if ($("input[data-value=personal]").attr("checked")) {
                $("#titlemsg").hide();
                return true;
            } else if ($("input[data-value=company]").attr("checked")) {
                if ($.trim($("#txtInvoiceTitle").val()) == "") {
                    $("#titlemsg").show();
                    $("#txtInvoiceTitle").focus();
                    return false;
                }
                else {
                    $("#titlemsg").hide();
                    return true;
                }
            }
        },
        ValidateAll: function () {
            return this.InvoiceTitleIsEmpty() && AreaIsEmpty() && this.DetailIsEmpty() && this.PostIsInvalid() && this.NameIsEmpty() && this.TelIsInvalid();
        },
        //-----------------切换-----------------------
        showInvoiceWin: function () {
            ShowBack();
            $("#applyInvoice").show();
        },
        hideInvoiceWin: function () {
            HideBack();
            $("#applyInvoice").hide();
        },
        showInvoiceInfoWin: function () {
            ShowBack();
            $("#applyInvoiceInfo").show();
        },
        hideInvoiceInfoWin: function () {
            HideBack();
            $("#applyInvoiceInfo").hide();
        },
        ShowSubmitWin: function () {
            if (this.ValidateAll()) {
                this.PassInvoice();
                this.hideInvoiceWin();
                this.showInvoiceInfoWin();
                $("#editInvoice").show();
            }
        },
        EditInvoice: function () {
            this.hideInvoiceInfoWin();
            this.showInvoiceWin();
        },
        //----------------赋值------------------------
        PassInvoice: function () {
            var title = "";
            if ($("input[data-value=personal]").attr("checked")) {
                title = $("input[data-value=personal]").val();
            } else if ($("input[data-value=company]").attr("checked")) {
                title = $("input[data-value=company]").val() + ":" + $.trim($("#txtInvoiceTitle").val());
            }
            $("#invoiceInfoTitle").html(title);

            var data = "";
            $("input[name=invoiceData]").each(function () {
                if ($(this).attr("checked")) {
                    data = $(this).val();
                    return false;
                }
            });
            $("#invoiceInfoData").html(data);

            var addr = "";
            addr = $("#province option[selected=selected]").html() + $("#city option[selected=selected]").html();
            var districtValue = $("#district").val();
            addr += $("#district option[value=" + districtValue + "]").html();
            addr += $("#txtStreet").val();

            if ($.trim($("#txtZip").val()) != "") {
                addr += "，邮编：" + $("#txtZip").val();
            }
            $("#invoiceInfoAddr").html(addr);

            var name = "";
            name = $("#txtUserName").val();
            $("#invoiceInfoUserName").html(name);

            var phone = "";
            phone = $("#txtCellphone").val();
            var zoneTemp = $.trim($("#txtTelZone").val()) == "" ? "区号" : $.trim($("#txtTelZone").val());
            var teleTemp = $.trim($("#txtTelephone").val()) == "" ? "电话号码" : $.trim($("#txtTelephone").val());
            var extensionTemp = $.trim($("#txtTelExtension").val()) == "" ? "分机号(可选)" : $.trim($("#txtTelExtension").val());
            var trimPhone = zoneTemp + "-" + teleTemp + "-" + extensionTemp;
            if (trimPhone != "区号-电话号码-分机号(可选)") {
                if (phone != "") {
                    phone += "、";
                }
                phone += $.trim($("#txtTelZone").val()) + "-" + $.trim($("#txtTelephone").val());
                if ($.trim($("#txtTelExtension").val()) && $.trim($("#txtTelExtension").val()) != "分机号(可选)") {
                    phone += "-" + $.trim($("#txtTelExtension").val());
                }
            }
            $("#invoiceInfoPhone").html(phone);

        },
        ConfirmInvoice: function () {
            var title = $("#invoiceInfoTitle").html();
            var data = $("#invoiceInfoData").html();
            var addr = $("#invoiceInfoAddr").html();
            var name = $("#invoiceInfoUserName").html();
            var phone = $("#invoiceInfoPhone").html();
            var soId = $("#SOID").val();
            $.BENLAI.ajax({
                type: "POST",
                url: "/UserHome/ApplyInvoice?SOId=" + encodeURIComponent(soId) + "&phone=" + encodeURIComponent(phone) + "&address=" + encodeURIComponent(addr)
                    + "&productName=" + encodeURIComponent(data) + "&invoiceName=" + encodeURIComponent(title) + "&userName=" + encodeURIComponent(name),
                cache: false,
                async: false,
                success: function (data) {
                    HideBack();
                    $("#applyInvoiceInfo").hide();
                    if (AjaxValidate(data)) {
                        if (data == "1") {
                            alert("申请发票成功！");
                        } else if (data == "0") {
                            alert("申请发票失败！");
                        } else {
                            alert(data);
                        }
                    }
                },
                error: function () { }
            });

        }
    });
});

